package de.fhwf.mixedreality.client2008;

import de.fhwf.mixedreality.client.IClient;

class MessageParser { 
 private PositionsStorage mPositions;
 
 private boolean mOpponentUpdated[];
 private boolean mTeammateUpdated[];
 private int mMyID;
 private boolean mIAmLost;
 private double mTime;
 private boolean mGameIsOn;
 private int mGoals[];
 private String mPlayMode;

 public MessageParser(PositionsStorage positions) {
  mPositions = positions;
  mOpponentUpdated = new boolean[IClient.MAX_PLAYERS];
  mTeammateUpdated = new boolean[IClient.MAX_PLAYERS];
  mGoals = new int[2];
  mPlayMode = "play on";
 }
 
 void parse(String data) {
  // We suppose we are not lost
  mIAmLost = false;

  // Initialize update checklist
  for (int i = 0; i < IClient.MAX_PLAYERS; i++) {
   mOpponentUpdated[i] = false;
   mTeammateUpdated[i] = false;
  }

  XMLProtocol.WorldDataApplication(this, mPositions, data);
 }

 double getTime() {
  return mTime;
 }
 
 boolean isGameOn() {
  return mGameIsOn;
 }
 
 int getMyID() {
  return mMyID;
 }

 int getTeamGoals() {
  return mGoals[0];
 }

 int getOpponentGoals() {
  return mGoals[1];
 }
 
 boolean isLost() {
  return mIAmLost;
 }
 
 // Tells if a given opponent's position was updated from last message
 boolean getOpponentUpdateState(int i) {
  if (i >= 0 && i < IClient.MAX_PLAYERS) {
   return mOpponentUpdated[i];
  }
  return false;
 }

 // Tells if a given teammate's position was updated from last message
 boolean getTeammateUpdateState(int i) {
  if (i >= 0 && i < IClient.MAX_PLAYERS) {
   return mTeammateUpdated[i];
  }
  return false;
 }

 String getPlayMode() {
	return mPlayMode; 
 }
 
 public void setLost(boolean lost) {
	 mIAmLost = lost;
 }

 public void setTime(int time) {
	 mTime = time;
 }

 public void setID(int id) {
	mMyID = id; 
 }

 public void setScore(int yellow, int blue) {
	 mGoals[0] = yellow;
	 mGoals[1] = blue;	 
 }

 public void setOpponentUpdated(int i) {
	 mOpponentUpdated[i] = true;
 }

 public void setTeammateUpdated(int i) {
	 mTeammateUpdated[i] = true;
 }

 public void setPlayMode(String mode) {
	mPlayMode = mode;
  if (mode.equals("play on") || mode.equals("warn ending")) {
	 mGameIsOn = true;
	} else {
	 mGameIsOn = false;
	}
 } 
}